package com.example.demo.day.config.learn.builder.x1;

public class Builder {

    static class Student {
        String name = null;
        int number = -1;
        String sex = null;
        int age = -1;
        String school = null;

        //构建器，利用构造器作为参数来构建student对象
        static class StudentBuilder {
            String name = null;
            int number = -1;
            String sex = null;
            int age = -1;
            String school = null;

            public StudentBuilder setName(String name) {
                this.name = name;
                return this;
            }

            public StudentBuilder setNumber(int number) {
                this.number = number;
                return this;
            }

            public StudentBuilder setSex(String sex) {
                this.sex = sex;
                return this;
            }

            public StudentBuilder setAge(int age) {
                this.age = age;
                return this;
            }

            public StudentBuilder setSchool(String school) {
                this.school = school;
                return this;
            }

            public Student build() {
                return new Student(this);
            }
        }

        public Student(StudentBuilder builder) {
            this.name = builder.name;
            this.age = builder.age;
            this.number = builder.number;
            this.school = builder.school;
            this.sex = builder.sex;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", number=" + number +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    ", school='" + school + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        Student a = new Student.StudentBuilder().setAge(13).setName("ss").build();
        Student b = new Student.StudentBuilder().setName("aa").setAge(17).setNumber(10000).setSchool("chunhui").setSex("man").build();
        System.out.println(a.toString());
        System.out.println(b.toString());
    }
}
